﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using GameStudio.IDE.Common.Events;
using Microsoft.Windows.Controls.Ribbon;
using GameStudio.Common;
using Microsoft.Practices.ServiceLocation;
using Utils.EventAggregator;
using GameStudio.IDE.Common;
using GameStudio.Framework.GraphicsEngine;
using GameStudio.IDE.Common.Events.Graphics_Designer;
using GameStudio.CodeGeneration;

namespace GameStudio.IDE.Controls
{
    [TemplatePart(Name = "uxRibbon", Type = typeof(Ribbon))]
    [TemplatePart(Name = "btnEmptyProject", Type = typeof(RibbonButton))]
    [TemplatePart(Name = "btnClassLibraryProject", Type = typeof(RibbonButton))]
    [TemplatePart(Name = "btnOpenProject", Type = typeof(RibbonButton))]
    [TemplatePart(Name = "btnAddNewItem", Type = typeof(RibbonButton))]

    [TemplatePart(Name = "newProjectMenuItem", Type = typeof(RibbonApplicationMenuItem))]
    [TemplatePart(Name = "openProjectMenuItem", Type = typeof(RibbonApplicationMenuItem))]
    [TemplatePart(Name = "saveProjectMenuItem", Type = typeof(RibbonApplicationMenuItem))]
    [TemplatePart(Name = "saveAsMenuItem", Type = typeof(RibbonApplicationMenuItem))]
    [TemplatePart(Name = "closeProjectMenuItem", Type = typeof(RibbonApplicationMenuItem))]
    [TemplatePart(Name = "exitMenuItem", Type = typeof(RibbonApplicationMenuItem))]

    [TemplatePart(Name = "groupItem", Type = typeof(RibbonGroup))]

    [TemplatePart(Name = "btnRunGame", Type = typeof(RibbonButton))]

    #region Graphics Designer Buttons

    [TemplatePart(Name = "tabGraphicsDesigner", Type = typeof(RibbonTab))]

    [TemplatePart(Name = "btnCube", Type = typeof(RibbonButton))]
    [TemplatePart(Name = "btnSphere", Type = typeof(RibbonButton))]

    [TemplatePart(Name = "btnColor", Type = typeof(RibbonButton))]
    [TemplatePart(Name = "btnTexture", Type = typeof(RibbonButton))]

    [TemplatePart(Name = "btnSave", Type = typeof(RibbonButton))]
    [TemplatePart(Name = "btnLoad", Type = typeof(RibbonButton))]

    [TemplatePart(Name = "btnClear", Type = typeof(RibbonButton))]
    [TemplatePart(Name = "btnHelp", Type = typeof(RibbonButton))]

    #endregion


    public class RibbonContainer :Control
    {
        #region Members

        Ribbon uxRibbon;

        #region Menu Items

        RibbonApplicationMenuItem newProjectMenuItem;
        RibbonApplicationMenuItem openProjectMenuItem;
        RibbonApplicationMenuItem saveProjectMenuItem;
        RibbonApplicationMenuItem saveAsMenuItem;
        RibbonApplicationMenuItem closeProjectMenuItem;
        RibbonApplicationMenuItem exitMenuItem;

        #endregion

        #region Start Buttons
        
        RibbonButton btnEmptyProject;
        RibbonButton btnClassLibraryProject;
        RibbonButton btnSaveProject;
        RibbonButton btnSaveAs;
        RibbonButton btnCloseProject;
        RibbonButton btnOpenProject;
        RibbonButton btnAddNewItem;
        RibbonButton btnOpenItem;
        RibbonButton btnAddExistingItem;

        RibbonButton btnRunGame;

        #endregion

        RibbonGroup groupItem;

        #region Graphics Buttons

        RibbonTab tabGraphicsDesigner;

        RibbonButton btnCube;
        RibbonButton btnSphere;
        RibbonButton btnColor;
        RibbonButton btnTexture;
        RibbonButton btnSave;
        RibbonButton btnLoad;
        RibbonButton btnClear;
        RibbonButton btnHelp;

        #endregion

        #endregion

        #region Constructor
        
        //public ProjectFactory CurProjectFactory;
        public RibbonContainer()
        {
            var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            eventAggregator.GetEvent<CreateProjectEvent>().Subscribe(MakeGroupItemsVisible);
            eventAggregator.GetEvent<FocusOnGraphicsTabEvent>().Subscribe(OnAddNewModel);
        }

        #endregion

        private void MakeGroupItemsVisible(GameStudio.IDE.Common.Project project)
        {
            groupItem.Visibility = System.Windows.Visibility.Visible;
        }

        private void OnAddNewModel(string str)
        {
            uxRibbon.SelectedItem = tabGraphicsDesigner;
            tabGraphicsDesigner.Visibility = System.Windows.Visibility.Visible;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            groupItem = (RibbonGroup)GetTemplateChild("groupItem");

            uxRibbon = (Ribbon)GetTemplateChild("uxRibbon");

            #region MenuItem

            newProjectMenuItem = (RibbonApplicationMenuItem)GetTemplateChild("newProjectMenuItem");
            newProjectMenuItem.Click += new RoutedEventHandler(newProjectMenuItem_Click);

            openProjectMenuItem = (RibbonApplicationMenuItem)GetTemplateChild("openProjectMenuItem");
            openProjectMenuItem.Click += new RoutedEventHandler(openProjectMenuItem_Click);

            saveProjectMenuItem = (RibbonApplicationMenuItem)GetTemplateChild("saveProjectMenuItem");
            saveProjectMenuItem.Click += new RoutedEventHandler(saveProjectMenuItem_Click);

            saveAsMenuItem = (RibbonApplicationMenuItem)GetTemplateChild("saveAsMenuItem");
            saveAsMenuItem.Click += new RoutedEventHandler(saveAsMenuItem_Click);

            closeProjectMenuItem = (RibbonApplicationMenuItem)GetTemplateChild("closeProjectMenuItem");
            closeProjectMenuItem.Click += new RoutedEventHandler(closeProjectMenuItem_Click);

            exitMenuItem = (RibbonApplicationMenuItem)GetTemplateChild("exitMenuItem");
            exitMenuItem.Click += new RoutedEventHandler(exitMenuItem_Click);

            btnRunGame = (RibbonButton)GetTemplateChild("btnRunGame");
            btnRunGame.Click += new RoutedEventHandler(btnRunGame_Click);

            btnAddExistingItem = (RibbonButton)GetTemplateChild("btnAddExistingItem");
            btnAddExistingItem.Click += new RoutedEventHandler(btnAddExistingItem_Click);

            #endregion

            #region Start
            
            btnEmptyProject = (RibbonButton)GetTemplateChild("btnEmptyProject");
            btnEmptyProject.Click += new RoutedEventHandler(btnEmptyProject_Click);

            btnClassLibraryProject = (RibbonButton)GetTemplateChild("btnClassLibraryProject");
            btnClassLibraryProject.Click += new RoutedEventHandler(btnClassLibraryProject_Click);

            btnAddExistingItem = (RibbonButton)GetTemplateChild("btnAddExistingItem");
            btnAddExistingItem.Click += new RoutedEventHandler(btnAddExistingItem_Click);

            btnSaveProject = (RibbonButton)GetTemplateChild("btnSaveProject");
            btnSaveProject.Click += new RoutedEventHandler(btnSaveProject_Click);

            btnSaveAs = (RibbonButton)GetTemplateChild("btnSaveAs");
            btnSaveAs.Click += new RoutedEventHandler(btnSaveAs_Click);

            btnCloseProject = (RibbonButton)GetTemplateChild("btnCloseProject");
            btnCloseProject.Click += new RoutedEventHandler(btnCloseProject_Click);

            btnOpenProject = (RibbonButton)GetTemplateChild("btnOpenProject");
            btnOpenProject.Click += new RoutedEventHandler(btnOpenProject_Click);

            btnAddNewItem = (RibbonButton)GetTemplateChild("btnAddNewItem");
            btnAddNewItem.Click += new RoutedEventHandler(btnAddNewItem_Click);

            //btnOpenItem = (RibbonButton)GetTemplateChild("btnOpenItem");
            //btnOpenItem.Click += new RoutedEventHandler(btnOpenItem_Click);
            #endregion

            #region Graphics Designer

            tabGraphicsDesigner = (RibbonTab)GetTemplateChild("tabGraphicsDesigner");

            btnCube = (RibbonButton)GetTemplateChild("btnCube");
            btnCube.Click += new RoutedEventHandler(btnCube_Click);

            btnSphere = (RibbonButton)GetTemplateChild("btnSphere");
            btnSphere.Click += new RoutedEventHandler(btnSphere_Click);

            btnColor = (RibbonButton)GetTemplateChild("btnColor");
            btnColor.Click += new RoutedEventHandler(btnColor_Click);

            btnTexture = (RibbonButton)GetTemplateChild("btnTexture");
            btnTexture.Click += new RoutedEventHandler(btnTexture_Click);

            btnSave = (RibbonButton)GetTemplateChild("btnSave");
            btnSave.Click += new RoutedEventHandler(btnSave_Click);

            btnLoad = (RibbonButton)GetTemplateChild("btnLoad");
            btnLoad.Click += new RoutedEventHandler(btnLoad_Click);

            btnClear = (RibbonButton)GetTemplateChild("btnClear");
            btnClear.Click += new RoutedEventHandler(btnClear_Click);

            btnHelp = (RibbonButton)GetTemplateChild("btnHelp");
            btnHelp.Click += new RoutedEventHandler(btnHelp_Click);

            #endregion
        }

        #region Event Handlers

        #region Start

        void btnAddExistingItem_Click(object sender, RoutedEventArgs e)
        {
            OpenItemControl OpenItem = new OpenItemControl();
        }

        void btnCloseProject_Click(object sender, RoutedEventArgs e)
        {
        }

        void btnEmptyProject_Click(object sender, RoutedEventArgs e)
        {
            //wndNewProject newproject = new wndNewProject("emptyProject1","Empty Project");
            wndNewProject newproject = new wndNewProject();
            newproject.Show();
        }

        void btnClassLibraryProject_Click(object sender, RoutedEventArgs e)
        {
            //wndNewProject newproject = new wndNewProject("classLibraryProject1", "Class Project");
            var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            eventAggregator.GetEvent<NewClassLibraryEvent>().Publish("");

            wndNewProject newproject = new wndNewProject();
            newproject.Show();
        }

        void btnSaveProject_Click(object sender, RoutedEventArgs e)
        {
            //SaveProjectControl SaveProject = new SaveProjectControl();
            GenerateEmptyClass emptyClass = new GenerateEmptyClass();
            emptyClass.Generate();

            string PathOfXmlFile = System.IO.Path.Combine(Session.CurrentProject.Path, "XMLfile.xml");

            GameStudio.Common.Serilization.SerilizationHelper.Serilize(Session.CurrentProject,
                PathOfXmlFile);
        }

        void btnSaveAs_Click(object sender, RoutedEventArgs e)
        {
        }

        void btnOpenItem_Click(object sender, RoutedEventArgs e)
        {
            OpenItemControl openItem = new OpenItemControl();
        }

        void btnAddNewItem_Click(object sender, RoutedEventArgs e)
        {
            WndAddNewItem wnd = new WndAddNewItem();
            wnd.Show();
        }

        void btnNewProject_Click(object sender, RoutedEventArgs e)
        {
            wndNewProject wnd = new wndNewProject();
            wnd.Show();
        }

        void btnOpenProject_Click(object sender, RoutedEventArgs e)
        {
            OpenProjectcontrol OpenProject = new OpenProjectcontrol();
            //var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            //   EventAgg.EventAggregator.GetEvent<NewProjectEvent>().Publish(CurProjectFactory);
        }

        void btnRunGame_Click(object sender, RoutedEventArgs e)
        {
            LoadAsm asm = new LoadAsm("EmptyClass", "EmptyClass", "Main");
        }

        #endregion

        #region MenuItems

        void newProjectMenuItem_Click(object sender, RoutedEventArgs e)
        {
            wndNewProject wnd = new wndNewProject();
            wnd.Show();
        }

        void openProjectMenuItem_Click(object sender, RoutedEventArgs e)
        {
            OpenProjectcontrol OpenProject = new OpenProjectcontrol();
            //var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            //   EventAgg.EventAggregator.GetEvent<NewProjectEvent>().Publish(CurProjectFactory);
        }

        //void btnRunGame_Click(object sender, RoutedEventArgs e)
        //{
        //    MyProgram.Game();
        //}

        void saveProjectMenuItem_Click(object sender, RoutedEventArgs e)
        {

        }

        void saveAsMenuItem_Click(object sender, RoutedEventArgs e)
        {

        }

        void closeProjectMenuItem_Click(object sender, RoutedEventArgs e)
        {

        }

        void exitMenuItem_Click(object sender, RoutedEventArgs e)
        {

        }

        #endregion

        #region Graphics Designer

        void btnHelp_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.MessageBox.Show(
                "Press shift to delete." 
                +"\n"
                +"Right Click + move mouse to rotate."
                +"\n"
                + "Right Click + alt to zoom.");
        }

        void btnClear_Click(object sender, RoutedEventArgs e)
        {
            var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            eventAggregator.GetEvent<ClearEvent>().Publish("");
        }

        void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            eventAggregator.GetEvent<LoadModelEvent>().Publish("");
        }

        void btnSave_Click(object sender, RoutedEventArgs e)
        {
            var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            eventAggregator.GetEvent<SaveModelEvent>().Publish("");
        }

        void btnTexture_Click(object sender, RoutedEventArgs e)
        {
            var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            eventAggregator.GetEvent<AddTextureEvent>().Publish("");            
        }

        void btnColor_Click(object sender, RoutedEventArgs e)
        {
            var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            eventAggregator.GetEvent<ChangeColorEvent>().Publish("");
        }

        void btnSphere_Click(object sender, RoutedEventArgs e)
        {
            var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            eventAggregator.GetEvent<AddSphereEvent>().Publish("");
        }

        void btnCube_Click(object sender, RoutedEventArgs e)
        {
            var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            eventAggregator.GetEvent<AddCubeEvent>().Publish("");   
        }

        #endregion

        #endregion

    }
}
